package cn.itcast.demo;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class LuceneIndexUpdateTest {


    /**
     *
     * 修改和删除时要注意，id对应的类型，如果是数值类型的id，一定要使用数值类型的查询去匹配
     * @throws IOException
     */
    @Test
    public void testCreateIndex() throws IOException {

        //定义目录类  open 可以自动匹配环境
        Directory directory = FSDirectory.open(new File("indexDir"));

        //创建分词器
        Analyzer analyzer = new IKAnalyzer();

        //创建索引写入的配置对象
        IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, analyzer);


        //索引写入工具
        IndexWriter indexWriter = new IndexWriter(directory, conf);

        //删除所有
        //indexWriter.deleteAll();

        //根据词条删除，

        //根据查询删除

        //根据id查询的条件
        Query query = NumericRangeQuery.newLongRange("id",3L,3L,true,true);

        //把查询条件传入indexWriter对象中，
        indexWriter.deleteDocuments(query);

        indexWriter.commit();

        indexWriter.close();

    }
}
